import { Entity, PrimaryGeneratedColumn, Column,  } from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";

@Entity("casino_config")
@ManualTimestamp()
export class CasinoConfig {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: "varchar", length: 64, comment: "配置键" })
  configKey!: string;

  @Column({ type: "text", comment: "配置值" })
  configValue!: string;

  @Column({ type: "varchar", length: 255, comment: "配置描述", nullable: true })
  description?: string;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;
@Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;
// 获取配置值的方法
  getValueAsNumber(): number {
    return parseInt(this.configValue, 10) || 0;
  }

  getValueAsBoolean(): boolean {
    return this.configValue.toLowerCase() === "true";
  }

  getValueAsString(): string {
    return this.configValue;
  }

  // 设置配置值的方法
  setValue(value: string | number | boolean): void {
    if (typeof value === "boolean") {
      this.configValue = value.toString();
    } else if (typeof value === "number") {
      this.configValue = value.toString();
    } else {
      this.configValue = value;
    }
  }

  // 检查是否为数值类型配置
  get isNumeric(): boolean {
    return !isNaN(Number(this.configValue));
  }

  // 检查是否为布尔类型配置
  get isBoolean(): boolean {
    const value = this.configValue.toLowerCase();
    return value === "true" || value === "false";
  }

  // 获取配置类型
  get configType(): "string" | "number" | "boolean" {
    if (this.isBoolean) return "boolean";
    if (this.isNumeric) return "number";
    return "string";
  }
}
